package com.goods.business.service.imp;

import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.service.ConsumerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.vo.business.ConsumerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ConsumerServiceImpl implements ConsumerService {
    @Resource
    private ConsumerMapper consumerMapper;

    @Override
    public List<ConsumerVO> findConsumerList(Consumer consumer) {
        List<Consumer> consumerList = consumerMapper.selectByConsumer(consumer);
        List<ConsumerVO> collect = consumerList.stream().map(consumer1 -> {
            ConsumerVO consumerVO = new ConsumerVO();
            //复制 被复制的数据类型要完全包含复制的
            BeanUtils.copyProperties(consumer1, consumerVO);
            return consumerVO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public Integer addConsumer(Consumer consumer) {
        Date date = new Date();
        consumer.setCreateTime(date);
        consumer.setModifiedTime(date);
        return consumerMapper.insert(consumer);
    }

    @Override
    public ConsumerVO edit(Long id) {
        Consumer consumer = consumerMapper.selectByPrimaryKey(id);
        ConsumerVO consumerVO = new ConsumerVO();
        BeanUtils.copyProperties(consumer, consumerVO);
        return consumerVO;
    }

    @Override
    public Integer update(Long id, Consumer consumer) {
        consumer.setId(id);
        Date date = new Date();
        consumer.setModifiedTime(date);
        return consumerMapper.updateByPrimaryKeySelective(consumer);
    }

    @Override
    public Integer delete(Long id) {
        return consumerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ConsumerVO> findAll() {
        List<Consumer> consumerList = consumerMapper.selectAll();
        List<ConsumerVO> collect = consumerList.stream().map(consumer -> {
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVO);
            return consumerVO;
        }).collect(Collectors.toList());
        return collect;
    }
}
